package com.ts.conch.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ts.conch.common.result.CommonResult;
import com.ts.conch.dao.ItsPermissionCategoryDao;
import com.ts.conch.dao.ItsPermissionDao;
import com.ts.conch.entity.TsPermission;
import com.ts.conch.entity.TsPermissionCategory;
import com.ts.conch.service.ItsPermissionService;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
public class ItsPermissionServiceImpl extends ServiceImpl<ItsPermissionDao, TsPermission> implements ItsPermissionService {
    @Autowired
    private ItsPermissionDao permissionDao;
    @Autowired
    private ItsPermissionCategoryDao categoryDao;

    /** 日志打印工具 **/
    private static final Logger logger = LoggerFactory.getLogger(ItsPermissionServiceImpl.class);

    @Override
    public List<TsPermission> queryAllPermission() {
        logger.info("[权限管理] - 程序[ItsPermissionServiceImpl.queryAllPermission] - 开始运行 - 查询全部资源:{}", "");
        List<TsPermission> permissionList = permissionDao.queryAllLrPermission();
        logger.info("[权限管理] - 程序[ItsPermissionServiceImpl.queryAllPermission] - 结束运行 - 返回结果:{}", JSON.toJSON(permissionList));
        return permissionList;
    }

    @Override
    public CommonResult<?> create(TsPermission permission) {
        logger.info("[权限管理] - 程序[ItsPermissionServiceImpl.create] - 开始运行 - 新增资源permission:{}", JSON.toJSON(permission));
        CommonResult<?> commonResult = CommonResult.failed("系统异常,新增资源失败!");
        int result = permissionDao.insert(permission);
        if (1 == result) {
            commonResult = CommonResult.success(permission, "新增资源成功!");
        }
        logger.info("[权限管理] - 程序[ItsPermissionServiceImpl.create] - 开始运行 - 返回结果:{}", JSON.toJSON(permission));
        return commonResult;
    }

    @Override
    public CommonResult<?> update(TsPermission permission) {
        logger.info("[权限管理] - 程序[ItsPermissionServiceImpl.update] - 开始运行 - 更新资源permission:{}", JSON.toJSON(permission));
        CommonResult<?> commonResult = CommonResult.failed("系统异常,修改失败!");
        int result = permissionDao.updateById(permission);
        if (1 == result) {
            commonResult = CommonResult.success(permission, "修改成功!");
        }
        logger.info("[权限管理] - 程序[ItsPermissionServiceImpl.update] - 开始运行 - 返回结果:{}", JSON.toJSON(permission));
        return commonResult;
    }

    @Override
    public CommonResult<?> getItem(String id) {
        logger.info("[权限管理] - 程序[ItsPermissionServiceImpl.getItem] - 开始运行 - 资源详情id:{}", id);
        CommonResult<?> commonResult = CommonResult.failed("系统异常,修改失败!");
        TsPermission permission = permissionDao.selectById(id);
        if (permission != null) {
            commonResult = CommonResult.success(permission, "修改成功!");
        }
        logger.info("[权限管理] - 程序[ItsPermissionServiceImpl.getItem] - 开始运行 - 返回结果:{}", JSON.toJSON(commonResult));
        return commonResult;
    }

    @Override
    public CommonResult<?> delete(String id) {
        logger.info("[权限管理] - 程序[ItsPermissionServiceImpl.delete] - 开始运行 - 删除资源id:{}", id);
        CommonResult<?> commonResult = CommonResult.failed("系统异常,修改失败!");
        int result = permissionDao.deleteById(id);
        if (result == 1) {
            commonResult = CommonResult.success("删除成功!");
        }
        logger.info("[权限管理] - 程序[ItsPermissionServiceImpl.getItem] - 开始运行 - 返回结果:{}", JSON.toJSON(commonResult));
        return commonResult;
    }

    @Override
    public CommonResult<?> list(Map<String, Object> params) {
        logger.info("[权限管理] - 程序[ItsPermissionServiceImpl.list] - 开始运行 - 分页查询params:{}", JSON.toJSON(params));
        CommonResult<?> commonResult = CommonResult.failed("系统异常,查询资源列表失败!");
        LambdaQueryWrapper<TsPermission> queryWrapper = new LambdaQueryWrapper<TsPermission>();
        String page = (String) params.get("page");
        String size = (String) params.get("size");
        String cateId = (String) params.get("cateId");
        String name = (String) params.get("name");
        String url = (String) params.get("url");
        IPage<TsPermission> pageParams = new Page<TsPermission>();
        pageParams.setCurrent(Long.parseLong(page));
        pageParams.setSize(Long.parseLong(size));
        queryWrapper.eq(TsPermission::getCategoryId, Long.parseLong(cateId));
        if (StringUtils.isEmpty(name)) {
            queryWrapper.like(TsPermission::getName, name);
        }
        if (StringUtils.isEmpty(name)) {
            queryWrapper.like(TsPermission::getUrl, url);
        }
        IPage<TsPermission> pageList = permissionDao.selectPage(pageParams, queryWrapper);
        if (CollectionUtil.isNotEmpty(pageList.getRecords())) {
            commonResult = CommonResult.success(pageList);
        }
        logger.info("[权限管理] - 程序[ItsPermissionServiceImpl.list] - 开始运行 - 返回结果:{}", JSON.toJSON(commonResult));
        return commonResult;
    }

    @Override
    public CommonResult<List<TsPermissionCategory>> listAll() {
        logger.info("[权限管理] - 程序[ItsPermissionServiceImpl.listAll] - 开始运行 - 按类别查询所有资源:{}", "");
        LambdaQueryWrapper<TsPermissionCategory> queryWrapper = new LambdaQueryWrapper<TsPermissionCategory>();
        List<TsPermissionCategory> categoryList = categoryDao.selectList(queryWrapper);
        if (CollectionUtil.isNotEmpty(categoryList)) {
            categoryList.stream().forEach(category -> {
                category.setTitle(category.getName());
                LambdaQueryWrapper<TsPermission> queryWrapper1 = new LambdaQueryWrapper<TsPermission>();
                queryWrapper1.eq(TsPermission::getCategoryId, category.getId());
                List<TsPermission> children = permissionDao.selectList(queryWrapper1);
                if (CollectionUtil.isNotEmpty(children)) {
                    children.stream().forEach(permission -> {
                        permission.setTitle(permission.getName());
                    });
                    category.setChildren(children);
                } else {
                    children = new ArrayList<TsPermission>();
                    category.setChildren(children);
                }
            });
        }
        logger.info("[权限管理] - 程序[ItsPermissionServiceImpl.listAll] - 开始运行 - 返回结果:{}", JSON.toJSON(categoryList));
        return CommonResult.success(categoryList);
    }

    @Override
    public List<TsPermission> queryAdminPermission(String adminId) {
        logger.info("[权限管理] - 程序[ItsPermissionServiceImpl.queryAdminPermission] - 开始运行 - 查询指定用户的资源adminId:{}", adminId);
        List<TsPermission> permissionList = permissionDao.queryAdminPermission(adminId);
        logger.info("[权限管理] - 程序[ItsPermissionServiceImpl.queryAdminPermission] - 开始运行 - 返回结果:{}", JSON.toJSON(permissionList));
        return permissionList;
    }
}
